<template>
  <div class="app-container">
    <el-dialog :title="'图纸上传'+'（'+form.orderNo+'）'" :visible.sync="dialogVisible" width="30%" append-to-body>
      <el-form ref="form" :model="form" v-loading="loading">
        <el-form-item label="附件" prop="fileUrl" required>
          <div v-if="form.fileUrl" style="display: flex; align-items: center;cursor: pointer;">
            <el-link type="primary">{{ form.fileName }}</el-link>
            <i class="el-icon-close closeDiv" @click="form.fileName=null;form.fileUrl=null"></i>
          </div>
          <el-upload
            v-else
            :headers="headers"
            :action="action"
            :on-success="uploadSuccess"
            :on-progress="uploadProgress"
            :show-file-list="false"
          >
            <el-button type="primary" plain>点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-loading="loading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getToken} from "../../../../utils/auth";
import {payCustomOrder} from "../../../../api/imsMng";
import {uploadSaleOrderFile} from "../../../../api/saleMng";

export default {
  dicts: ['ims_bank_name'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      form: {
        fileUrl: '',
        fileName: ''
      },
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      action: process.env.VUE_APP_BASE_API + '/ims/product/file/upload'
    }
  },
  methods: {
    show(form) {
      this.dialogVisible = true
      this.form = {}
      this.resetForm('form')
      this.form.id = form.id
      this.form.orderNo = form.orderNo
    },
    uploadProgress() {
      this.loading = true
    },
    uploadSuccess(res) {
      this.loading = false
      if (res.code != 200) {
        this.form.fileUrl = ''
        this.$message.error(res.msg)
        return
      }
      this.$set(this.form, 'fileUrl', res.data.fileUrl)
      this.$set(this.form, 'fileName', res.data.fileName)
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.loading = true
          uploadSaleOrderFile(this.form).then(res => {
            this.$emit('ok')
            this.dialogVisible = false
            this.$message.success('上传成功')
          }).finally(e => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>
<style scoped>
.closeDiv {
  color: red;
  margin-left: 10px
}
</style>
